﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartSpace.Core
{
    public partial class CoreService : ICoreService
    {
        #region SiteTemplate

        public bool SaveSiteTemplate(SiteTemplate template)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                if (context.Set<SiteTemplate>()
                    .Where(p => p.Id == template.Id)
                    .Select(p => new { p.Id })
                    .FirstOrDefault() == null)
                {
                    context.Set<SiteTemplate>().Add(template);
                }
                else
                {
                    context.Entry(template).State = System.Data.Entity.EntityState.Modified;
                }

                if (context.SaveChanges() > 0)
                {
                    return true;
                }
                return false;
            }
        }

        public List<SiteTemplate> GetSiteTemplates()
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                return context.Set<SiteTemplate>().ToList();
            }
        }

        public List<SiteTemplate> GetSiteTemplates(string type)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                return context.Set<SiteTemplate>().Where(p => p.Type == type).ToList();
            }
        }

        public SiteTemplate GetSiteTemplate(Guid id)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                return context.Set<SiteTemplate>().Where(p => p.Id == id).FirstOrDefault();
            }
        }
        #endregion
    }
}
